

$(function () {
    // $('.form-horizontal select').selectpicker({
    //     noneSelectedText: '请选择',
    //     sanitize: true,
    //     size: 8
    // });
    $('.form-horizontal select').selectpicker('refresh')
})




function submitFollow() {
    $('.form-horizontal').bootstrapValidator({
        fields: {
            contactName: {
                validators: {
                    notEmpty: {
                        message: '联系人姓名不能为空'
                    },

                }
            },
            contactInformation: {
                validators: {
                    notEmpty: {
                        message: '联系方式不能为空'
                    }
                }
            },
            followWay: {
                validators: {
                    notEmpty: {
                        message: '跟进方式不能为空'
                    }
                }
            },
            followDetail: {
                validators: {
                    notEmpty: {
                        message: '跟进详情不能为空'
                    }
                }
            },
            checkDetail: {
                validators: {
                    notEmpty: {
                        message: '跟进详情必选'
                    }
                }
            },

        },
    })
    $(".form-horizontal").bootstrapValidator('validate');//提交验证
    if (!$(".form-horizontal").data('bootstrapValidator').isValid()) {//获取验证结果，如果成功，执行下面代码

        return;
    }

    if ($("#checkDetail").val()=='简短' && $("#followDetail").val().length<50) {
        promptInfo("跟进详情不能少于50字!");
        return;
    }

    if ($("#checkDetail").val()=='详细' && $("#followDetail").val().length<200) {
        promptInfo("跟进详情不能少于200字!");
        return;
    }

    let jsonData={
        customerId:$("#customerIdName").val(), //客户id
        customerName:$("#customerIdName").text(), //客户名称
        contactId:$("#contactName").val(), //联系人id
        contactName:$("#contactName option:selected").text(), //联系人姓名
        contactInformation:$("#contactInformation").val(),
        followWay:$("#followWay").val(),
        followDetail:$("#followDetail").val()

    }
    console.log(jsonData)
    $.ajax({
        url: '/internalTrainingManager/follow',
        type: 'POST',
        dataType: "json",
        // contentType: "application/json;charset=utf-8",
        data: jsonData,
        success:function (res) {
            if(res.code=='000000'){

                // $.alert('操作成功')
                $.alert({
                    title: '提示',
                    content: '操作成功',
                    buttons: {
                        confirm: {
                            text: '确认',
                            btnClass: 'btn-primary',
                            action: function () {

                                var $thisTabs = parent.$('.mt-nav-bar .nav-tabs').find('li.active');
                                var navTab = parent.$multitabs._getNavTab($thisTabs);

                                closeTab(navTab);
                                openTab("我的客户池","/front/internalTrainingManager/myClientPool");

                                delStorageAndRemove(navTab);
                            }
                        }
                    }
                })

            }else{
                $.alert(res.message)
            }
        },
        error:function (err) {
            $.alert(err.message)
        }
    })
}

$('#addContact').on('hide.bs.modal', function () {
    if($('.form-horizontal').data('bootstrapValidator')!=undefined){
        $(".form-horizontal").data('bootstrapValidator').destroy();
        $('.form-horizontal').data('bootstrapValidator', null);
    }
})
// 简短
function clickBrief() {
    $("#detailsMany").attr('checked',false)
    $("#followDetail").attr('placeholder','请输入不少于50字')

    if($("#detailsMany").prop('checked')!=false || $("#detailsBrief").prop('checked')!=false) {

        $("#checkDetail").val('简短')
    }else{

        $("#checkDetail").val('')
    }
    if($('.form-horizontal').data('bootstrapValidator')!=undefined){
        $('.form-horizontal').data('bootstrapValidator')
            .updateStatus('checkDetail', 'NOT_VALIDATED',null)
            .validateField('checkDetail');
    }

}
// 详细
function clickMany() {
    $("#detailsBrief").attr('checked',false)
    $("#followDetail").attr('placeholder','请输入不少于200字')

    if($("#detailsMany").prop('checked')!=false || $("#detailsBrief").prop('checked')!=false) {

        $("#checkDetail").val('详细')
    }else{

        $("#checkDetail").val('')
    }
    if($('.form-horizontal').data('bootstrapValidator')!=undefined){
        $('.form-horizontal').data('bootstrapValidator')
            .updateStatus('checkDetail', 'NOT_VALIDATED',null)
            .validateField('checkDetail');
    }

}

// /internalTrainingManager/getNxContactByOwner 获取客户下联系人，参数 customerId


$.ajax({
    url: '/internalTrainingManager/getNxContactByOwner',
    type: 'POST',
    dataType: "json",
    // contentType: "application/json;charset=utf-8",
    data: {
        customerId:$("#customerIdName").val(), //客户id
    },
    success:function (res) {
        if(res.code=='000000'){


            let OptionHtml = '<option value="">请选择</option>';
            res.data.map(function (item) {
                OptionHtml += '<option value="' + item.contactId + '">' + item.contactName + '</option>';
            });
            $('#contactName').html("");
            $('#contactName').append(OptionHtml);
            $('#contactName').selectpicker('refresh');


        }else{
            $.alert(res.message)
        }
    },
    error:function (err) {
        $.alert(err.message)
    }
})


function promptInfo(content) {
    $.alert({
        title: '温馨提示',
        content: content,
        buttons: {
            confirm: {
                text: '确认',
                btnClass: 'btn-primary'
            }
        }
    });
}

function resetThing() {
    $(".reset-clear input").val('')
    $(".reset-clear select").val('')
    $('.reset-clear select').selectpicker('refresh')
    $(".reset-clear textarea").val('')
    $(".reset-clear input[type=checkbox]").attr('checked',false)
    if($('.form-horizontal').data('bootstrapValidator')!=undefined){
        $(".form-horizontal").data('bootstrapValidator').destroy();
        $('.form-horizontal').data('bootstrapValidator', null);

    }
}